﻿using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace GemTowerDefence.Towers
{
  public class Aquamarine : Tower
  {
    // A list of directions that the tower can shoot in.
    private readonly Vector2[] _directions = new Vector2[8];
    // All the enimes that are in range of the tower.
    private readonly List<Enemy> _targets = new List<Enemy>();

    public override bool HasTarget
    {
      // The tower will never have just one target.
      get { return false; }
    }

    /// <summary>
    /// Constructs a new Spike Tower object.
    /// </summary>
    public Aquamarine(Texture2D texture, Texture2D bulletTexture, Vector2 position)
      : base(texture, bulletTexture, position)
    {
      Damage = 20; // Set the damage.
      Range = 55; // Set the radius.

      // Store a list of all the directions the tower can shoot.
      _directions = new[]
            {
                new Vector2(-1, -1), // North West
                new Vector2( 0, -1), // North
                new Vector2( 1, -1), // North East
                new Vector2(-1,  0), // West
                new Vector2( 1,  0), // East
                new Vector2(-1,  1), // South West
                new Vector2( 0,  1), // South
                new Vector2( 1,  1), // South East
            };
    }

    public override void GetClosestEnemy(List<Enemy> enemies)
    {
      // Do a fresh search for targets.
      _targets.Clear();

      // Loop over all the enemies.
      foreach (Enemy enemy in enemies)
      {
        // Check wether this enemy is in shooting distance.
        if (IsInRange(enemy.Center))
        {
          // Make it a target.
          _targets.Add(enemy);
        }
      }
    }

    public override void Update(GameTime gameTime)
    {
      base.Update(gameTime);

      // Decide if it is time to shoot.
      if (MissileTimer >= 0.55f && _targets.Count != 0)
      {
        // For every direction the tower can shoot,
        for (int i = 0; i < _directions.Length; i++)
        {
          // create a new bullet that moves in that direction.
          var bullet = new Missile(MissileTexture, 
            Vector2.Subtract(Center, new Vector2(MissileTexture.Width / 2F)), _directions[i], 6, Damage);
          Missiles.Add(bullet);
        }
        MissileTimer = 0;
      }

      // Loop through all the bullets.
      for (int i = 0; i < Missiles.Count; i++)
      {
        var bullet = Missiles[i];
        bullet.Update(gameTime);

        // Kill the bullet when it is out of range.
        if (!IsInRange(bullet.Center))
        {
          bullet.Kill();
        }

        // Loop through all the possible targets
        for (int t = 0; t < _targets.Count; t++)
        {
          // If this bullet hits a target and is in range,
          if (_targets[t] != null && Vector2.Distance(bullet.Center, _targets[t].Center) < 12)
          {
            // hurt the enemy.
            _targets[t].CurrentHealth -= bullet.Damage;
            bullet.Kill();

            // This bullet can't kill anyone else.
            break;
          }
        }

        // Remove the bullet if it is dead.
        if (bullet.IsDead())
        {
          Missiles.Remove(bullet);
          i--;
        }
      }
    }
  }
}
